﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;


public partial class AddClient : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Admin admin;
    List<Admin> admins;
    Client client;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            //if (Session["AdminUser"] != null)
            //    SessionManager.CurrentUser = (LoggedInUser)Session["AdminUser"];
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {

                if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.Admin) ||
               Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.SuperAdmin))
                {
                    bindAdmin();
                    if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.Admin))
                    {
                        drpAdmin.SelectedValue = Convert.ToString(cookie["AdminId"]);
                        drpAdmin.Enabled = false;
                    }
                    if (Request.QueryString["Id"] != null)
                    {
                        txtUsername.Enabled = false;
                        fillClient(Request.QueryString["Id"].ToString());
                    }
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
        }
    }

    private void fillClient(string clientId)
    {
        client = new Client(_dataObject);
        client.Id = Convert.ToInt32(clientId);
        client.GetClientById();

        if (client.Id != 0)
        {

            drpAdmin.SelectedValue = Convert.ToString(client.AdminId);
            txtFirstName.Text = client.FirstName;
            txtLastName.Text = client.LastName;
            txtPhone.Text = client.Phone;
            txtEmailAddress.Text = client.Email;
            txtAddress.Text = client.Address;
            txtCity.Text = client.City;
            txtState.Text = client.State;
            txtPincode.Text = client.Zip;
            txtUsername.Text = client.Username;
            txtPassword.Attributes.Add("value", client.Password);
            txtConfirmPassword.Attributes.Add("value", client.Password);
            txtCommission.Text = client.CommissionRate;
            txtNECommission.Text = client.NECommission;
            txtLimit.Text = client.ActualLimit.ToString();
            txtMinLimit.Text = client.MinLimit.ToString();
            txtMaxLimit.Text = client.MaxLimit.ToString();
            if (client.IsActive == true)
            {
                rdoActive.SelectedValue = "1";
            }
            else
            {
                rdoActive.SelectedValue = "0";
            }
        }
    }

    private void bindAdmin()
    {
        admin = new Admin(_dataObject);
        admins = new List<Admin>();
        admins = admin.GetAllAdmin();
        if (admins.Count > 0)
        {
            drpAdmin.DataSource = admins;
            drpAdmin.DataTextField = "FirstName";
            drpAdmin.DataValueField = "Id";
            drpAdmin.DataBind();
            drpAdmin.Items.Insert(0, new ListItem("Select Admin", "0"));
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        client = new Client(_dataObject);
        client.AdminId = Convert.ToInt32(drpAdmin.SelectedValue);
        client.FirstName = txtFirstName.Text;
        client.LastName = txtLastName.Text;
        client.Phone = txtPhone.Text;
        client.Email = txtEmailAddress.Text;
        client.Address = txtAddress.Text;
        client.City = txtCity.Text;
        client.State = txtState.Text;
        client.Zip = txtPincode.Text;
        client.Username = txtUsername.Text;
        client.Password = txtPassword.Text;
        client.CommissionRate = txtCommission.Text;
        client.NECommission = txtNECommission.Text;
        client.Limit = Convert.ToDecimal(txtLimit.Text);
        client.MatchTempLimit = Convert.ToDecimal(txtLimit.Text);
        client.ActualLimit = Convert.ToDecimal(txtLimit.Text);
        client.MinLimit = Convert.ToDecimal(txtMinLimit.Text);
        client.MaxLimit = Convert.ToDecimal(txtMaxLimit.Text);
        client.IsLogin = false;
        //client.IsActive = true;
        //client.IsDeleted = false;
        if (rdoActive.SelectedValue == "1")
        {
            client.IsActive = true;
            client.IsDeleted = false;
        }
        else
        {
            client.IsActive = false;
            client.IsDeleted = true;
        }
        bool userNameExist = CheckUserNameExist(txtUsername.Text);
        if (Request.QueryString["Id"] != null)
        {
            if (!userNameExist)
            {
                client.Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                client.DaeUpdated = System.DateTime.UtcNow;
                client.Update();
                clearControl();
                Response.Redirect("ViewClient.aspx");

                //layerSuccess.InnerHtml = "<strong>Success!</strong> Client updated successfully.";
                //layerSuccess.Attributes.Add("style", "display:block;");
                //layerError.Attributes.Add("style", "display:none;");
            }
            else
            {
                layerError.InnerHtml = "<strong>Error!</strong> Email already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
        }
        else
        {
            if (!userNameExist)
            {
                client.DateAdded = System.DateTime.UtcNow;
                client.Create();
                clearControl();
                layerSuccess.InnerHtml = "<strong>Success!</strong> Client added successfully.";
                layerSuccess.Attributes.Add("style", "display:block;");
                layerError.Attributes.Add("style", "display:none;");
            }
            else
            {
                layerError.InnerHtml = "<strong>Exist!</strong> Username already exist.";
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
        }

    }

    private bool CheckUserNameExist(string username)
    {
        bool isExist = false;
        int ClientId = 0;
        if (Convert.ToString(Request.QueryString["Id"]) != "" && Request.QueryString["Id"] != null)
        {
            ClientId = Convert.ToInt32(Request.QueryString["Id"]);
        }
        List<Client> clients = new List<Client>();
        clients = client.CheckUserNameExist(ClientId, username);
        if (clients.Count > 0)
        {
            isExist = true;
        }
        else
        {
            isExist = false;
        }
        return isExist;
    }

    private void clearControl()
    {
        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtEmailAddress.Text = string.Empty;
        txtAddress.Text = string.Empty;
        txtCity.Text = string.Empty;
        txtState.Text = string.Empty;
        txtPincode.Text = string.Empty; ;
        txtUsername.Text = string.Empty;
        txtPassword.Text = string.Empty;
        txtCommission.Text = string.Empty;
    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewClient.aspx");
    }
}